<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <!-- //按钮区域 -->
        <el-col>
          <el-button type="primary" @click="showAddCateDialo"
            >添加分类</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table
        class="tree-table"
        :data="goodsCateList"
        :columns="columns"
        show-index
        index-text="#"
        :selection-type="false"
        :expand-type="false"
        border
        :show-row-hover="false"
      >
        <template slot="isShow" slot-scope="scope">
          <i
            v-if="!scope.row.cat_deleted"
            class="el-icon-success"
            style="color:green"
          ></i>
          <i v-else class="el-icon-error" style="color:red"></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level == 0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level == 1" type="success"
            >二级</el-tag
          >
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <template slot="opt" slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="editCateByid(scope.row.cat_id)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="deleteCateByid(scope.row.cat_id)"
            >删除</el-button
          >
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类dialog -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="clearCate"
    >
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            clearable
            v-model="selectKeys"
            :options="parentCate"
            :props="{
              expandTrigger: 'hover',
              checkStrictly: 'true',
              value: 'cat_id',
              label: 'cat_name',
              children: 'children'
            }"
            @change="parentHandleChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改分类dialog -->
    <el-dialog title="修改分类" :visible.sync="editDialogVisible" width="50%">
      <el-form
        :model="editCateFrom"
        :rules="addCateFormRules"
        ref="editCateFromRef"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="editCateFrom.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCateInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "Cate",
  data() {
    return {
      // 查询条件
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5
      },
      // 商品数据存放
      goodsCateList: [],
      //商品数据的条数
      total: 0,
      //父级节点的数据
      parentCate: [],
      // table数据的显示
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          type: "template",
          template: "isShow"
        },
        {
          label: "排序",
          type: "template",
          template: "order"
        },
        {
          label: "操作",
          type: "template",
          template: "opt"
        }
      ],
      // 是否显示添加分类dialog
      addCateDialogVisible: false,
      // 是否显示编辑修改分类dialog
      editDialogVisible: false,
      // 存放添加分类的数据
      addCateForm: {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0
      },
      // 保存需要修改的分类数据
      editCateFrom: {},
      // 父级分类级联选择展示的数据
      // parentProps: { value: "cat_id", label: "cat_name", children: "children" },
      // 验证规则
      addCateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称!", trigger: "blur" }
        ]
      },
      //选中的父级分类的id数组
      selectKeys: []
    };
  },
  methods: {
    //获取商品列表数据
    async getGoodsCate() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo
      });
      if (res.meta.status == 200) {
        this.goodsCateList = res.data.result;
        this.total = res.data.total;
      }
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsCate();
    },
    handleCurrentChange(newPag) {
      this.queryInfo.pagenum = newPag;
      this.getGoodsCate();
    },
    // 添加分类点击函数
    showAddCateDialo() {
      this.getPrantCateList();
      this.addCateDialogVisible = true;
    },
    // 获取父级分类
    async getPrantCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 }
      });
      if (res.meta.status == 200) {
        this.parentCate = res.data;
      } else {
        this.$message.error("获取父节点失败!");
      }
    },
    // 父级菜单级联选择器发生改变的时候
    parentHandleChange() {
      // console.log(this.selectKeys);
      if (this.selectKeys.length > 0) {
        // 父级id
        this.addCateForm.cat_pid = this.selectKeys[this.selectKeys.length - 1];
        // 新增当前级数
        this.addCateForm.cat_level = this.selectKeys.length;
      } else {
        // 父级id
        this.addCateForm.cat_pid = 0;
        // 新增当前级数
        this.addCateForm.cat_level = 0;
      }
    },
    // 确认添加
    addCate() {
      this.$refs.addCateFormRef.validate(async vid => {
        if (vid) {
          const { data: res } = await this.$http.post(
            "categories",
            this.addCateForm
          );
          if (res.meta.status == 201) {
            this.$message.success("添加分类成功!");
            this.getGoodsCate();
            this.addCateDialogVisible = false;
          } else {
            this.$message.error("添加分类失败!");
          }
        }
      });
    },
    // 重置清空
    clearCate() {
      this.$refs.addCateFormRef.resetFields();
      this.selectKeys = [];
      this.addCateForm.cat_level = 0;
      this.addCateForm.cat_pid = 0;
      this.addCateForm.cat_name = "";
    },
    // 根据id编辑修改分类
    async editCateByid(id) {
      const { data: res } = await this.$http.get("categories/" + id);
      if (res.meta.status == 200) {
        this.editCateFrom = res.data;
        // console.log(this.editCateFrom);
      }
      this.editDialogVisible = true;
    },
    // 提交编辑分类
    editCateInfo() {
      this.$refs.editCateFromRef.validate(async vid => {
        if (vid) {
          const { data: res } = await this.$http.put(
            "categories/" + this.editCateFrom.cat_id,
            {
              id: this.editCateFrom.cat_pid,
              cat_name: this.editCateFrom.cat_name
            }
          );
          if (res.meta.status == 200) {
            this.$message.success("更新分类数据成功!");
            this.getGoodsCate();
            this.editDialogVisible = false;
          } else {
            this.$message.error("更新分类数据失败!");
          }
        }
      });
    },
    // 根据id删除分类
    async deleteCateByid(id) {
      const confirmRlust = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      // console.log(confirmRlust);
      //如果用户点了确定泽返回字符串confirm,选择了取消返回字符串cancel
      if (confirmRlust === "confirm") {
        const { data: res } = await this.$http.delete("categories/" + id);
        if (res.meta.status == 200) {
          this.$message.success("删除分类成功!");
          this.getGoodsCate();
        } else {
          this.$message.error("删除分类失败!");
          this.getGoodsCate();
        }
      } else {
        this.$message.info("取消了删除!");
      }
    }
  },
  created() {
    this.getGoodsCate();
  },
  components: {},
  computed: {},
  beforeMount() {},
  mounted() {},
  watch: {}
};
</script>
<style lang="less" scoped>
.tree-table {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
